<template>
  <div class="chat-message--input is-focused">
    <resizable-text-area
      id="chat-input"
      ref="chatInput"
      v-model="userInput"
      aria-label="Type your message"
      placeholder="Type your message"
      class="form-input user-message-input"
      @typing-off="onTypingOff"
      @typing-on="onTypingOn"
      @focus="onFocus"
      @blur="onBlur"
    />
    <div class="button-wrap">
        <button
          type="submit"
          class="icon-button flex items-center justify-center ml-1"
          @click="handleButtonClick()"
        >
          <fluent-icon icon="send" :style="`color: ${color}`" />
        </button>
      </div>
  </div>
</template>

<script>
import FluentIcon from './FluentIcon/Index.vue'
import ResizableTextArea from './ResizableTextArea.vue'
import { mapActions } from 'vuex'
import eventListenerMixins from '../mixins/eventListenerMixins'
import { buildHotKeys } from '../helpers/KeyboardHelpers'
export default {
  name: 'ChatInputWrap',
  components: {
    FluentIcon,
    ResizableTextArea
  },
  mixins: [eventListenerMixins],
  data () {
    return {
      userInput: '',
      showEmojiPicker: false,
      isFocused: false,
      color: '#009CE0'// '#6e6f73'
    }
  },
  computed: {
    showSendButton () {
      return this.userInput.length > 0
    },
    emojiIconColor () {
      return this.showEmojiPicker
        ? `text-woot-500 ${this.$dm('text-black-900', 'dark:text-slate-100')}`
        : `${this.$dm('text-black-900', 'dark:text-slate-100')}`
    }
  },
  mounted () {
    // Donot use the keyboard listener mixin here as the events here are supposed to be
    // working even if input/textarea is focussed.
    document.addEventListener('keydown', this.handleKeyEvents)
  },
  destroyed () {
    document.removeEventListener('keydown', this.handleKeyEvents)
  },
  methods: {
    ...mapActions('conversation', [
      'sendMessage'
    ]),
    onBlur () {
      this.isFocused = false
    },
    onFocus () {
      this.isFocused = true
    },
    handleButtonClick () {
      if (this.userInput && this.userInput.trim()) {
        const content = this.userInput
        this.sendMessage({ content })
      }
      this.userInput = ''
      this.focusInput()
    },
    handleEnterKeyPress (e) {
      if (e.keyCode === 13 && !e.shiftKey) {
        e.preventDefault()
        this.handleButtonClick()
      }
    },
    toggleEmojiPicker () {
      this.showEmojiPicker = !this.showEmojiPicker
    },
    hideEmojiPicker (e) {
      if (this.showEmojiPicker) {
        e.stopPropagation()
        this.toggleEmojiPicker()
      }
    },
    emojiOnClick (emoji) {
      this.userInput = `${this.userInput}${emoji} `
    },
    onTypingOff () {
      this.toggleTyping('off')
    },
    onTypingOn () {
      this.toggleTyping('on')
    },
    toggleTyping (typingStatus) {
      this.$store.dispatch('conversation/toggleUserTyping', { typingStatus })
    },
    focusInput () {
      this.$refs.chatInput.focus()
    },
    isAValidEvent () {
      return true
      // return (
      //   !this.showUserMentions &&
      //   !this.showMentions &&
      //   !this.showCannedMenu &&
      //   !this.showVariablesMenu &&
      //   this.isFocused &&
      //   isEditorHotKeyEnabled(this.uiSettings, selectedKey)
      // )
    },
    handleKeyEvents (e) {
      const keyCode = buildHotKeys(e)
      if (
        ['meta+enter', 'ctrl+enter'].includes(keyCode) &&
        this.isAValidEvent('cmd_enter')
      ) {
        this.handleButtonClick()
      }
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" scoped>
@import '../assets/scss/_variables.scss';
@import '../assets/scss/_mixins.scss';

h3 {
margin: 40px 0 0;
}
ul {
list-style-type: none;
padding: 0;
}
li {
display: inline-block;
margin: 0 10px;
}
a {
color: #42b983;
}
.chat-message--input {
  align-items: center;
  display: flex;
  padding: 0 $space-small 0 $space-slab;
  border-radius: 7px;

  &.is-focused {
    box-shadow: 0 0 0 1px $color-woot, 0 0 2px 3px $color-primary-light;
  }
}

.emoji-dialog {
  right: $space-one;
}

.button-wrap {
  display: flex;
  align-items: center;
  // padding-left: $space-small;
}
.form-input {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    background: #fff;
    border: 1px solid #e0e6ed;
    border-radius: .1875px;
    box-sizing: border-box;
    color: #3c4858;
    display: block;
    font-family: Inter,-apple-system,system-ui,BlinkMacSystemFont,Segoe UI,Roboto,Helvetica Neue,Tahoma,Arial,sans-serif;
    font-size: 1rem;
    height: 2.5rem;
    line-height: 1.5;
    max-width: 100%;
    outline: none;
    padding: .25rem;
    position: relative;
    transition: background .2s,border .2s,box-shadow .2s,color .2s;
    width: 100%;
}
.user-message-input {
  border: 0;
  height: $space-large;
  min-height: $space-large;
  max-height: 2.4 * $space-mega;
  resize: none;
  padding: 0;
  padding-top: $space-smaller;
  padding-bottom: $space-smaller;
  margin-top: $space-small;
  margin-bottom: $space-small;
}
textarea {
    overflow: auto;
}
.text-black-900 {
    --text-opacity: 1;
    color: #1b1c1f;
    color: rgba(27,28,31,var(--text-opacity));
}
.bg-white {
    --bg-opacity: 1;
    background-color: #fff;
    background-color: rgba(255,255,255,var(--bg-opacity));
}
.chat-message--input {
  align-items: center;
  display: flex;
  padding: 0 $space-small 0 $space-slab;
  border-radius: 7px;

  &.is-focused {
    box-shadow: 0 0 0 1px $color-woot, 0 0 2px 3px $color-primary-light;
  }
}
.button-wrap {
  display: flex;
  align-items: center;
  padding-left: $space-small;
}
*, :after, :before {
    box-sizing: border-box;
    border: 0 solid;
}
button {
    background-color: transparent;
    background-image: none;
}
</style>
